home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CPPTASK.ARJ / TEST.CPP < prev    next >
C/C++ Source or Header  |  1991-08-20  |  5KB  |  190 lines

  1. //
  2. // Test program for checking basic CPPTask functions.
  3. //
  4.  
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include <time.h>
  12. #include <dos.h>
  13.  
  14. #include "task.hpp"
  15. #include "tsklocal.hpp"
  16.  
  17.  
  18. #define STACKSIZE 2048
  19.  
  20. unsigned int _stklen = 5 * STACKSIZE;  /* Four tasks + main Task Stack */
  21.  
  22. void far task1 (void);
  23. local char stack1 [STACKSIZE];
  24. local task tcb1((funcptr)task1, (byteptr)stack1, STACKSIZE, PRI_STD, NULL);
  25.  
  26. void far task2 (void);
  27. local char stack2 [STACKSIZE];
  28. local task tcb2((funcptr)task2, (byteptr)stack2, STACKSIZE, PRI_STD, NULL);
  29.  
  30. void far task3 (void);
  31. local char stack3 [STACKSIZE];
  32. local task tcb3((funcptr)task3, (byteptr)stack3, STACKSIZE, PRI_STD, NULL);
  33.  
  34. void far task4 (void);
  35. local char stack4 [STACKSIZE];
  36. local task tcb4((funcptr)task4, (byteptr)stack4, STACKSIZE, PRI_STD, NULL);
  37.  
  38. // declare all necessary data structures for inter-task communication
  39.  
  40. typedef struct {
  41.                farptr xx;
  42.                char str [20];
  43.                } message;         // message structure for mailbox
  44.  
  45. byte pipbuf [10];                 // buffer area for pipe
  46. word bufbuf [40];                 // buffer area for buffer
  47.  
  48. mailbox box;                      // create mailbox ...
  49. message msg;                      //  and instance of message struct
  50. flag halt, clockflag;             // create flag
  51. pipe pip((farptr) pipbuf, sizeof(pipbuf));      // create pipe
  52. buffer buf((farptr) bufbuf, sizeof(bufbuf));    // create buffer
  53.  
  54. int endrun;
  55.  
  56. //
  57. // Task1 sends a mail to Task3, and waits for a response in the buffer.
  58. // The response is then displayed.
  59. // Task1 will stop while the halt flag is set.
  60. //
  61.  
  62. void far task1 (void)
  63. {
  64.    char str [20];
  65.  
  66.    printf ("Task 1 started\n");
  67.    while (!endrun)    // run till completion signalled
  68.       {
  69.       halt.wait_flag_clear (0L);
  70.  
  71.       t_delay (5L);   // delay for 5 clock ticks
  72.       printf ("Task 1 Sending Message to Task 3\n");
  73.       strcpy (msg.str, "From T1");
  74.       box.send_mail (&msg);
  75.  
  76.       buf.read_buffer (str, 20, 0L);
  77.       printf("Buffer data received %s \n", str);
  78.       }
  79. }
  80.  
  81. /*
  82.    Task2 reads the keyboard. If a character has been read, it is passed
  83.    to Task4 via a pipe. Entering 'h' will set the halt flag (stopping Task1),
  84.    entering 'c' will clear the halt flag.
  85.    'e' stops the program.
  86. */
  87.  
  88. void far task2 (void)
  89. {
  90.    int ch;
  91.  
  92.    printf ("Task 2 started\n");
  93.    while (!endrun)
  94.       {
  95.       ch = t_read_key () & 0xff;
  96.       switch (tolower (ch))
  97.          {
  98.          case 'h':   halt.set_flag ();
  99.                      break;
  100.          case 'c':   halt.clear_flag ();
  101.                      break;
  102.          case 'e':   main_tcb.wake_task ();
  103.                      break;
  104.          case 'b':   sound (2000);
  105.                      t_delay (2L);
  106.                      nosound ();
  107.                      break;
  108.          }
  109.  
  110.       if (!endrun)
  111.          pip.write_pipe ((char)ch, 0L);
  112.       putch ('2');
  113.       }
  114. }
  115.  
  116. /*
  117.    Task3 waits for mail, then sends it back through a buffer.
  118. */
  119.  
  120. void far task3 (void)
  121. {
  122.    message far *m;
  123.  
  124.    printf ("Task 3 started\n");
  125.    while (!endrun)
  126.       {
  127.       m = (message far *) box.wait_mail (0L);
  128.       printf ("Task 3 Msg received %s \n", m->str);
  129.       m->str [6] = '3';
  130.       buf.write_buffer (m->str, 7, 0L);
  131.       }
  132. }
  133.  
  134.  
  135. /*
  136.    Task4 waits for a character in the pipe and displays it. To make
  137.    things livelier, it uses a timeout while waiting, and will display
  138.    faces when the timeout occurred before the character.
  139. */
  140.  
  141. void far task4 (void)
  142. {
  143.    int ch;
  144.  
  145.    printf ("Task 4 started\n");
  146.    while (!endrun)
  147.       {
  148.       ch = pip.read_pipe (10L);
  149.       if (ch < 0)
  150.          putch (0x02);
  151.       else
  152.          printf ("Task 4 got - %c\n", ch);
  153.       }
  154. }
  155.  
  156. // NOTE, the scheduler and a few standard tasks are create automatically
  157. //    before the start of main()
  158.  
  159. int main (void)
  160. {
  161.    endrun = 0;
  162.  
  163.    tcb1.start_task ();          // start all tasks
  164.    tcb2.start_task();
  165.    tcb3.start_task ();
  166.    tcb4.start_task();
  167.  
  168.    tasker.preempt_on ();       // turn preemption on
  169.  
  170.    t_delay (0L);               // delay main task so that others can run
  171.  
  172. // if we get here something has cause the main task to run and we
  173. // will terminate processing
  174.  
  175.    endrun = 1;
  176.    puts ("******** Main Task *********");
  177.  
  178.    main_tcb.set_priority (10);  // lower priority to allow other tasks to
  179.                                 // termiate processing
  180.  
  181.    tasker.schedule ();          // explicit call to scheduler
  182.    tasker.preempt_off ();       // turn preemption off
  183.  
  184.    puts ("******** End Run *********");
  185.  
  186.    return 0;
  187. }
  188.  
  189.  
  190.